(function($){
	
	var info =null;

	var buildParams =function(open){
	    var params ={"BODY":{"WORN_SERV_CODE": "sShortAddMode","PHONE_NO": $("#cellphone").html(),"LOGIN_NO": getEmpCode(),"OPERATE_TYPE": open?"A":"D","PROD_PRCID": info.code,"LIMIT_CODE": "","PHONE_SLV": "","COMMON_INFO": {"PROVINCE_GROUP": "10008"}}};
	    return params;
	};
	
	
	var process = function(open){
		showLoadingBar($("#loadingbar"),true,false,"正在进行请求，请稍后……");
        var params = buildParams(open);
	  	
	 	$.ajax({
		    type: "post",
		    contentType: "application/json",
		    data: JSON.stringify(params),
		    url: Constants.rpcCrmHost+"s4000Cfm?token="+getToken(),
		    dataType: "json",
		    async: true,
		    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
		    	console.log("data = "+data);
		        console.info(data);
		        showLoadingBar($("#loadingbar"),false);
		        var service ="用户"+$("#cellphone").html()+' "'+$("#title").html()+'"'+"业务"+(open?"办理":"退订");
		        if(data.ROOT.RETURN_CODE!=0){
					var msg = data.ROOT.RETURN_MSG;
					if(reqUrl.indexOf("s4000Cfm")!=-1 && returnData.RETURN_CODE==1005){
						var dataStr = JSON.stringify(data);
						if(dataStr.indexOf("timed out")!=-1 || dataStr.indexOf("CRM_TEMPLATE")!=-1
							|| dataStr.indexOf("中间件")!=-1 || dataStr.indexOf("系统忙")!=-1){
							msg = ("调用服务超时，请核实是否办理成功。");
						}
					}
					alert(service+"失败:"+msg);
		        	console.log("业务办理失败:"+data.ROOT.RETURN_CODE+":"+data.ROOT.RETURN_MSG);
		        	return;
		        }
		        //办理成功，也提示
		        alert(service+"成功！");
		        

		    },
		    error : function(textStatus) {
		    	alert(service+"失败！服务器没有返回。");
		        console.info("request error:");
		    }
		  });
};

var callback=function(){
	hide($("#step_one"));
	show($("#step_two"));
	
	$("#cellphone").html($("#phone").val());
	
	//$("#cellphone").html("18380279446");
	
	($("#open")).on("click",function(){
		console.info("request to open");
		process(true);
	});
	
	($("#close")).on("click",function(){
		console.info("request to close");
		process(false);
	});
	
};
	
var loadDetails=function(){
	info = JSON.parse(getCookie("business_info"));
	console.info(info);
	$("#title").html(info.title);
    $("#summary").html(info.summary);
    $("#fee").html(info.fee);
    
    document.title=info.title; 
};

$(function(){
    	loadDetails();
    	
    	($("#getSMS")).on("click",function(){
    		sendSms($("#phone").val());
    	});
    	
    	($("#validate")).on("click",function(){
    		verifySms($("#phone").val(),$("#smscode").val(),callback);
    	});
    	
    	//callback();
  	
    });
  


})(jQuery);